<?php
class SmsAction extends Action{
	
	public function _initialize() {
			//初始化用户相关常量，例如用户购物车信息，用户地区信息（通过地区显示相关产品），最新礼物等
			//这些操作是否应放在，Action中完成？
			exit;
	}
	
	public function index() {
			//加载需要的数据 
			exit;
			//$this->display('smsdemo');
	}

	/**
	*	预览彩信图片
	**/
	public function preview() {
     	$smsimg = $_REQUEST['smsimg'];
     	$smstext = $_REQUEST['smstext'];
     	$xyOffset = $_REQUEST['xyOffset'];
     	$textcolor = $_REQUEST['textcolor'];
     	$ispreview = $_REQUEST['ispreview'];//是否生成后的保存图片
     	$smsicon = $_REQUEST['smsicon'];//表情图片路径
     	
     	$smsImgDao = service('SmsImg');
     	$smsImgDao->uid = $this->mid;
     	$imgdata_ = $smsImgDao->parseRequestData($xyOffset,$textcolor,$font_file,$font_size);
     	$im = $smsImgDao->writeTxtImg2Bg(SITE_PATH.getAttachUrl($smsimg),SITE_PATH.$smsicon
     															,$smstext,$imgdata_['xyOffset'],$imgdata_['rgb']
																,$imgdata_['font_file'],$imgdata_['font_size']);
     	
     	if($ispreview!='yes'){
			$tmp_smsimgPath = $smsImgDao->doSaveUserTmpTemplet($im,$smsimg);

			ImageDestroy($im);
			$result['tmp_smsimgPath']=$tmp_smsimgPath;
			$result['status']=1;
			echo json_encode( $result );
			exit;
		}
		 /** else{
		  		$tmp_smsimgPath = $smsImgDao->doSaveUserPreviewTemplet($im,$smsimg);

					ImageDestroy($im);
					$result['tmp_smsimgPath']=$tmp_smsimgPath;
					$result['status']=1;
					echo json_encode( $result );
					exit;
		  }
		  **/
			//echo "kim";
			// output image  
		Header ('Content-type: image/png');                 //即便是从jpg拷贝的图片，也能以png输出，  
		ImagePng ($im);  
			// clean up  
		ImageDestroy($im);   
		exit;
	}
	
	/**
	*	获取彩信图片模板
	**/
	public function getJsonSmsTemplet(){
			$sort_id = $_REQUEST['sort_id'];
			if(!empty($sort_id)&&$sort_id!='all')$where['sort_id']=$sort_id;
			$where['is_delete']='N';
			$where['is_uploadbyuser']='N';
			$limit = 4;
			$order = 'ctime desc';
			$smstdao = M('SmsTemplet');
			
			if($sort_id=='my'){
				unset($where['sort_id']);
				$where['is_uploadbyuser']='Y';
				$where['insert_user_id']=$this->mid;
			}
			// 查询总数
			$count_options['where']	=	$where;
			$count_options['limit'] = 1;
			$count_options['field'] = 'count(1) as count';
			// 去掉统计时的排序提高效率
			$result =	$smstdao->where($where)->limit(1)->field('count(1) as count')->find();
			//dump($result);
			$count	=	$result['count'];
			//echo 'count='.$count;
			//如果总数大于零，则进行进一步数据查询
			if($count > 0) {
					$p	=	new KPage($count,$limit,2);
					// 查询数据
					$shelflist = $smstdao->where($where)->order($order)
																	->limit($p->firstRow.','.$p->listRows)->findAll();
					//dump($vShelfPage);
					// 输出控制
					$output['count']			=	$count;
					$output['totalPages']	=	$p->totalPages;
					$output['totalRows']	=	$p->totalRows;
					$output['nowPage']		=	$p->nowPage;
					$output['html']				=	$p->show();
					$output['data']				=	$shelflist;
					unset($p);
					unset($count);
			}else{
					$output['count']		=	0;
					$output['totalPages']	=	0;
					$output['totalRows']	=	0;
					$output['nowPage']		=	1;
					$output['html']			=	'';
					$output['data']			=	'';
			}
			//dump($output);
			exit(json_encode($output)); 
	}
			
	//彩信图片上传处理
  	function smsimg(){
   		$type = $_REQUEST['t'];
  		$pSmsImg = service('SmsImg');
   		$pSmsImg ->uid = $this->mid;
    	if( $type == 'upload' ){
       		echo $pSmsImg->upload();
   		}elseif ( $type == 'save'){
       		$smsimg_relpath = $pSmsImg->dosave();
       		/** 如果生成成功，则保存相关数据 **/
       		if(!empty($smsimg_relpath)&&$this->mid > 0){
       				$map['title'] = $this->mid;
							$map['sort_id'] = 'empty';
					    $map['ctime'] 	=	time();
					    $map['is_uploadbyuser'] 	=	'Y';
					    $map['is_delete'] 	=	'N';
					    $map['insert_user_id'] 	=	$this->mid;
					    $map['imgpath'] = $smsimg_relpath;
					    $smstDao = M('SmsTemplet')->add($map);
       		}
       		
       		$result['smsimg_path']=$smsimg_relpath;
					$result['status']=1;
					echo json_encode( $result );
			}
  		exit;     		
  	}

}
